package cn.xdf.demo01;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.*;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.After;
import org.junit.Test;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.File;
import java.io.IOException;

public class Demo03_Query子类查询 {


    //索引库目录
    FSDirectory dir=FSDirectory.open(new File("D:/aaa/index"));

    //索引库读取对象
    DirectoryReader reader= IndexReader.open(dir);

    //索引库搜索对象
    IndexSearcher indexSearcher=new IndexSearcher(reader);

    //分词器
    IKAnalyzer analyzer = new IKAnalyzer();

    public Demo03_Query子类查询() throws IOException {
    }


    @After
    public void after()throws Exception{
        reader.close();
        dir.close();
    }

    public static void print(IndexSearcher indexSearcher,TopDocs topDocs) throws Exception{


        //保护所有的文档id和对应的分支
        ScoreDoc[] scoreDocs =topDocs.scoreDocs;

        for(ScoreDoc scoreDoc:scoreDocs){

            System.out.println("文档id:"+scoreDoc.doc);
            //根据文档id搜索出对应的文档
            Document doc=indexSearcher.doc(scoreDoc.doc);

            String id =doc.get("id");
            String name =doc.get("name");
            String title =doc.get("title");
            String price =doc.get("price");
            String pic =doc.get("pic");

            System.out.println("匹配分值："+scoreDoc.score);
            System.out.println("商品id:"+id);
            System.out.println("商品名称："+name);
            System.out.println("商品标题:"+title);
            System.out.println("商品价格:"+price);
            System.out.println("商品图片："+pic);
            System.out.println("-------------------");
        }
    }


    @Test
    public void test01() throws Exception{

        //封装条件,精确查询,不会对搜索条件进行分词
        TermQuery termQuery=new TermQuery(new Term("title","笔记本"));
        TopDocs topDocs = indexSearcher.search(termQuery, 100);

        print(indexSearcher,topDocs);
    }

    @Test
    public void test02() throws Exception{

        NumericRangeQuery<Double> query = NumericRangeQuery.newDoubleRange("price", 2199.0D, 4000.0D, false, true);

        TopDocs topDocs = indexSearcher.search(query, 100);

        print(indexSearcher,topDocs);

    }

    @Test
    public void test03() throws Exception{
        //条件1
        QueryParser queryParser=new QueryParser(Version.LUCENE_4_10_3,"title",analyzer);
        Query query_01 = queryParser.parse("华为笔记本");

        //条件2
        NumericRangeQuery<Double> query_02 = NumericRangeQuery.newDoubleRange("price", 4000.0D, 10000.0D, true, true);


        //组合条件
        BooleanQuery booleanClauses=new BooleanQuery();
        booleanClauses.add(query_01, BooleanClause.Occur.MUST);
        booleanClauses.add(query_02, BooleanClause.Occur.MUST);

        //搜索最终条件
        TopDocs topDocs = indexSearcher.search(booleanClauses, 100);

        print(indexSearcher,topDocs);

//查询华为笔记本，价格在4000-5000之间的
    }
}
